package Page.AboutUs;

import Dao.CurrentStudentDao;
import DataClass.CurrentStudent;
import Navigation.NavigationController;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import java.net.URL;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CurrentStudentPage {

    private static VBox detailBox;

    public static Scene getScene() {
        VBox studentMenuPane = new VBox(10);
        studentMenuPane.setPadding(new Insets(10));
        studentMenuPane.setAlignment(Pos.TOP_CENTER);
        Label label = new Label("在读学生信息");
        label.setStyle("-fx-font-size: 24px; -fx-font-weight: bold;");
        studentMenuPane.getChildren().add(label);

        try {
            List<CurrentStudent> data = CurrentStudentDao.getCurrentStudent();
            displayStudentsByYear(studentMenuPane, data);
        } catch (SQLException ignored) {
            System.err.println("数据库出现错误");
        }

        ScrollPane scrollPane = new ScrollPane(studentMenuPane);
        scrollPane.setFitToWidth(true);
        scrollPane.setFitToHeight(true);

        // 初始化详细信息区域
        detailBox = new VBox(10);
        detailBox.setPadding(new Insets(10));
        detailBox.setAlignment(Pos.TOP_LEFT);
        detailBox.setPrefWidth(300);  // 设置详细信息区域的宽度

        HBox rootPane = new HBox(10);
        rootPane.getChildren().addAll(scrollPane, detailBox);

        VBox vbox = new VBox();
        Button button = new Button("返回");
        button.setOnAction(event -> {
            NavigationController.back();
        });
        vbox.getChildren().add(button);
        vbox.getChildren().add(rootPane);

        return new Scene(vbox);
    }

    public static void displayStudentsByYear(VBox rootPane, List<CurrentStudent> students) {
        // 按入学年份分组学生，并将入学年份转换为字符串类型
        Map<Integer, List<CurrentStudent>> studentsByYear = students.stream()
                .collect(Collectors.groupingBy(student -> student.getSEnter()));

        for (int year : studentsByYear.keySet()) {
            Label yearLabel = new Label(String.valueOf(year + "年"));
            yearLabel.setStyle("-fx-font-size: 18px; -fx-font-weight: bold;");

            VBox studentYearBox = new VBox(10);
            studentYearBox.setAlignment(Pos.TOP_CENTER);

            for (CurrentStudent student : studentsByYear.get(year)) {
                HBox studentPane = new HBox(10);
                studentPane.setAlignment(Pos.CENTER);

                VBox photoAndNameBox = new VBox(5);
                photoAndNameBox.setAlignment(Pos.CENTER);
                photoAndNameBox.setMinWidth(300); // 设置最小宽度为300

                // 加载并设置照片
                URL imageUrl = CurrentStudentPage.class.getResource("/res/" + student.getPhotoPath());
                ImageView photoView = new ImageView(new Image(imageUrl.toString()));
                photoView.setFitHeight(150);
                photoView.setFitWidth(150);

                // 学生姓名标签
                Label nameLabel = new Label(student.getSCName());
                nameLabel.setFont(Font.font(14)); // 设置字体大小

                // 添加鼠标悬停效果
                photoView.setOnMouseEntered(event -> {
                    photoView.setOpacity(0.7);
                    photoView.getScene().setCursor(Cursor.HAND);
                });
                photoView.setOnMouseExited(event -> {
                    photoView.setOpacity(1.0);
                    photoView.getScene().setCursor(Cursor.DEFAULT);
                });
                nameLabel.setOnMouseEntered(event -> {
                    nameLabel.setUnderline(true);
                    nameLabel.getScene().setCursor(Cursor.HAND);
                });
                nameLabel.setOnMouseExited(event -> {
                    nameLabel.setUnderline(false);
                    nameLabel.getScene().setCursor(Cursor.DEFAULT);
                });

                // 点击事件
                photoView.setOnMouseClicked(event -> showStudentDetails(student));
                nameLabel.setOnMouseClicked(event -> showStudentDetails(student));

                // 将照片和姓名放入垂直布局
                photoAndNameBox.getChildren().addAll(photoView, nameLabel);

                studentPane.getChildren().addAll(photoAndNameBox);
                studentYearBox.getChildren().add(studentPane);
            }

            rootPane.getChildren().addAll(yearLabel, studentYearBox);
        }
    }

    private static void showStudentDetails(CurrentStudent student) {
        detailBox.getChildren().clear(); // 清空详细信息区域内容

        Label idLabel = new Label("学号: " + student.getSNo());
        Label nameLabel = new Label("姓名: " + student.getSCName());
        Label genderLabel = new Label("性别: " + student.getSSex());
        Label categoryLabel = new Label("类别: " + student.getSType());
        Label nationalityLabel = new Label("国籍: " + student.getSNation());
        Label supervisorLabel = new Label("导师: " + student.getSTNo());
        Label supervisorLabel2 = new Label("副导师: " + student.getSATNo());

        // 设置详细信息标签的样式
        idLabel.setStyle("-fx-font-size: 18px; -fx-font-weight: bold;");
        nameLabel.setStyle("-fx-font-size: 18px;");
        genderLabel.setStyle("-fx-font-size: 18px;");
        categoryLabel.setStyle("-fx-font-size: 18px;");
        nationalityLabel.setStyle("-fx-font-size: 18px;");
        supervisorLabel.setStyle("-fx-font-size: 18px;");
        supervisorLabel2.setStyle("-fx-font-size: 18px;");
        detailBox.getChildren().addAll(idLabel, nameLabel, genderLabel, categoryLabel, nationalityLabel, supervisorLabel, supervisorLabel2);
    }
}
